package android.slc.box.core.notification;

import android.slc.box.commonlibrary.compat.ValueWrap;
import android.slc.box.core.property.WrapDataChangeListener;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.value.ObservableValue;

public interface NotificationObserver extends WrapDataChangeListener<Object[]>, de.saxsys.mvvmfx.utils.notifications.NotificationObserver {

    @Override
    default void changed(ObservableValue<? extends ValueWrap<Object[]>> observable, ValueWrap<Object[]> oldValue, ValueWrap<Object[]> newValue) {
        WrapDataChangeListener.super.changed(observable, oldValue, newValue);
        if (observable instanceof ReadOnlyProperty) {
            ReadOnlyProperty<?> readOnlyProperty = (ReadOnlyProperty<?>) observable;
            receivedNotification(readOnlyProperty.getName(), newValue == null ? null : newValue.getData());
        } else {
            receivedNotification(null, newValue == null ? null : newValue.getData());
        }
    }

    /**
     * 此处将不被调用，作废，已被此类的@changed()方法覆盖
     * @param value
     */
    @Deprecated()
    @Override
    default void onChanged(Object[] value) {

    }

    /**
     * 确定次观察者传递的通知类型是实现自当前类
     * @param notificationObserver
     * @return
     */
    static boolean ensureProtectedNotificationObserver(de.saxsys.mvvmfx.utils.notifications.NotificationObserver notificationObserver) {
        if (notificationObserver instanceof NotificationObserver) {
            return true;
        }
        throw new RuntimeException("请使用ProtectedViewModel.NotificationObserver");
    }

}